/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tb_out_car_register.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.tb_driver.entity.TbDriver;
import com.thinkgem.jeesite.modules.tb_driver.service.TbDriverService;
import com.thinkgem.jeesite.modules.tb_out_car_register.entity.TbOutCarRegister;
import com.thinkgem.jeesite.modules.tb_out_car_register.service.TbOutCarRegisterService;
import com.thinkgem.jeesite.modules.tb_waybill.entity.TbWaybill;
import com.thinkgem.jeesite.modules.tb_waybill.service.TbWaybillService;

/**
 * 出车登记Controller
 * @author ZhangYangZe
 * @version 2017-06-03
 */
@Controller
@RequestMapping(value = "${adminPath}/tb_out_car_register/tbOutCarRegister")
public class TbOutCarRegisterController extends BaseController {

	@Autowired
	private TbOutCarRegisterService tbOutCarRegisterService;
	@Autowired
	private TbWaybillService tbWaybillService;
	@Autowired
	private TbDriverService tbDriverService;
	
	@ModelAttribute
	public TbOutCarRegister get(@RequestParam(required=false) String id) {
		TbOutCarRegister entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbOutCarRegisterService.get(id);
		}
		if (entity == null){
			entity = new TbOutCarRegister();
		}
		return entity;
	}
	
	/**
	 * 登记记录列表
	 * @param tbOutCarRegister
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("tb_out_car_register:tbOutCarRegister:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbOutCarRegister tbOutCarRegister, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbOutCarRegister> page = tbOutCarRegisterService.findPage(new Page<TbOutCarRegister>(request, response), tbOutCarRegister); 
		model.addAttribute("page", page);
		return "modules/tb_out_car_register/tbOutCarRegisterList";
	}

	/**
	 * 登记页面
	 * @param tbOutCarRegister
	 * @param model
	 * @return
	 */
	@RequiresPermissions("tb_out_car_register:tbOutCarRegister:view")
	@RequestMapping(value = "form")
	public String form(TbOutCarRegister tbOutCarRegister, Model model) {
		model.addAttribute("tbOutCarRegister", tbOutCarRegister);
		return "modules/tb_out_car_register/tbOutCarRegisterForm";
	}

	/**
	 * 登记
	 * @param tbOutCarRegister
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
/*	@RequiresPermissions("tb_out_car_register:tbOutCarRegister:edit")
	@RequestMapping(value = "save")
	public String save(TbOutCarRegister tbOutCarRegister, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbOutCarRegister)){
			return form(tbOutCarRegister, model);
		}
		tbOutCarRegisterService.save(tbOutCarRegister);
		addMessage(redirectAttributes, "保存出车登记成功");
		return "redirect:"+Global.getAdminPath()+"/tb_out_car_register/tbOutCarRegister/?repage";
	}*/
	
	
	
	@RequiresPermissions("tb_out_car_register:tbOutCarRegister:edit")
	@RequestMapping(value = "delete")
	public String delete(TbOutCarRegister tbOutCarRegister, RedirectAttributes redirectAttributes) {
		tbOutCarRegisterService.delete(tbOutCarRegister);
		addMessage(redirectAttributes, "删除出车登记成功");
		return "redirect:"+Global.getAdminPath()+"/tb_out_car_register/tbOutCarRegister/?repage";
	}
	
	/**
	 * 出车登记列表
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("tb_out_car_register:tbOutCarRegister:view")
	@RequestMapping("tbOutCarRegisterList/tbOutCarRegister")
	public String tbOutCarRegister(HttpServletRequest request, HttpServletResponse response, Model model){
		TbWaybill tbWaybill = new TbWaybill();
		tbWaybill.setState("9");
		//Page<TbWaybill> page = tbWaybillService.findPage(new Page<TbWaybill>(request, response), tbWaybill); 
		List<TbWaybill> page=tbWaybillService.findList(tbWaybill);
		TbDriver driver = new TbDriver();
		driver.setDriverState("1");
		List<TbDriver> list = tbDriverService.findList(driver);
		model.addAttribute("page", page);
		model.addAttribute("driver", list);
		return "modules/tb_out_car_register/tbOutCarRegister";
	}
	
	@RequiresPermissions("tb_out_car_register:tbOutCarRegister:edit")
	@RequestMapping(value = "tbOutCarRegisterList/save")
	public String saveNew(TbOutCarRegister tbOutCarRegister, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbOutCarRegister)){
			return form(tbOutCarRegister, model);
		}
		tbOutCarRegisterService.saveNew(tbOutCarRegister);
		addMessage(redirectAttributes, "保存出车登记成功");
		return "redirect:"+Global.getAdminPath()+"/tb_out_car_register/tbOutCarRegister/?repage";
	}
	
	/**
	 * 获取运单id
	 * @param waybill_id
	 * @return
	 */
	@RequestMapping("driverList")
	@ResponseBody
	public List<TbDriver> driverList(String waybill_id){
		TbWaybill tbWaybill = tbWaybillService.get(waybill_id);
		List<TbDriver> list = new ArrayList<TbDriver>();
		TbDriver drivreOne = tbWaybill.getTbDriver();
		TbDriver driverTwo = tbWaybill.getTbDriverTwo();
		list.add(drivreOne);
		list.add(driverTwo);
		return list;
	}
}